"""Search movies for "Monty Python" and output the results.

Note: You should edit APIKEY to yours or you may be banned by service!
"""
#coding=utf-8
import douban.service
import xml.dom.minidom
from xml.dom.minidom import parseString
from django.http import HttpResponseRedirect
#from template import Render
from django.template.loader import get_template
from django.template import Context
from django.shortcuts import render_to_response
from movie.forms import MovieForm

# Please use your own api key instead. e.g. :
#APIKEY = "23eeeb4347bdd26bfc6b7ee9a3b755dd"
APIKEY = '0a68216cf667cabe1f386446e337603a'
SECRET = '1b262b1a30df95be'

client = douban.service.DoubanService(api_key=APIKEY)
#tpl = get_template("movie.html")
#print tpl
def PrintResp(request):
	keyword = request.GET.get('q')
	feed = client.SearchMovie(keyword.encode("utf-8"))
	html = ""
	movie_list = []
	for movie in feed.entry:
		average = parseString(str(movie.rating)).getElementsByTagNameNS("http://schemas.google.com/g/2005", "rating")[0].getAttribute("average")
		movie_list.append({'url':movie.GetAlternateLink().href, 'movie':movie.title.text.decode("utf-8"), 'rate':average})
	
	return render_to_response("movie.html",{"keyword":keyword, "movie_list":movie_list})
	#return HttpResponse(html)

def Welcome(request):
	return render_to_response("welcome.html")

def AddMovieSucceed(request):
	return render_to_response("addMovieSucceed.html")

def AddMovie(request):
	error = []
	if request.method == 'POST':
		if len(request.POST.get('name','')) > 0 and len(request.POST.get('rate','')) > 0:
			print "%s %.2f" % (request.POST.get('name',''), float(request.POST.get('rate','')))
			return HttpResponseRedirect('/addMovie/succeed/')
	return	render_to_response("addMovie.html",{'name':request.POST.get('name',''),'rate':request.POST.get('rate','')})
